# -*- coding: utf-8 -*-

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


import arcpy
from arcpy.sa import *
import numpy as np
import os

from arcgisscripting import Raster
from arcpy.sa import Con, IsNull

# set workspace
from points_to_dem import create_dem_from_points
from merge_underwater_terrain import merge_dems

# 设置工作空间和输入/输出文件
# arcpy_workspace_path = r"E:\W01"
arcpy_workspace_path = r"F:\test_arcpy_workspace"
arcpy.env.workspace = arcpy_workspace_path

# 输入DEM
original_dem = Raster(r"W01_DEM.tif")

underwater_terrain_path = "W01_underwater_terrain.tif"

# 以稀疏高程点生成地形文件

input_points = r"underwater_points.shp"
elevation_field = r"Elevation"  # 确保这是包含高程值的字段名
tin_output = r"elevation.tin"

cell_size = 1
create_dem_from_points(input_points, elevation_field, tin_output, underwater_terrain_path, cell_size,
                       workspace=arcpy_workspace_path)

underwater_terrain = Raster(underwater_terrain_path)

target_terrain_path = r"\W01_merged_terrain.tif"
#
merge_dems(arcpy_workspace_path, original_dem, underwater_terrain, arcpy_workspace_path+target_terrain_path)

merged_terrain = Raster(target_terrain_path)

# set plane height bound
plane_heights = np.arange(0, 5.01, 0.01)  # 0-10m,step:0.01m
print(plane_heights)

# Traverse each plane height
for plane_height in plane_heights:
    # output file name
    output_volume_raster = os.path.join(arcpy.env.workspace, "volume_at_height_{:.2f}.tif".format(plane_height))
    output_depth_raster = os.path.join(arcpy.env.workspace, "depth_at_height_{:.2f}.tif".format(plane_height))

    # Surface Volume
    arcpy.SurfaceVolume_3d(in_surface=merged_terrain, reference_plane="BELOW", base_z=plane_height,
                           out_text_file="\\surface_volume\\" + "W01_" + str(plane_height) + ".txt")

    print("Completed volume and depth calculations for plane height: %d meters" % plane_height)

print("All volume and depth calculations have been completed.")
